<?php

// Set language. default is 'me', the other option is 'eng'
$language = 'eng';

// Start Session
session_start();

// Include text strings
include('lang/' . $language . '.php');

include('lib/Sve_Funkcije.php');

// Set timezone
date_default_timezone_set('Europe/Podgorica');

// Define a variable equal to what is submitted in the guessing_form
$get_attempt = isset($_GET['attempt']) ? $_GET['attempt'] : null;


// Define a variable equal to the action value from forms
$get_action = isset($_GET['action']) ? $_GET['action'] : null;


// A minimum value for the random number
$random_number_from = 1;
// A maximum value for the random number
$random_number_to = 10;

// A date to pull for statistics
$ended = date('H:i:s');

// This number determines the range within the attempted number is Close to the chosen number
$range = 3;


// Instantiate an object
$objekat = new Sve_Funkcije();


// Define a variable equal to what is submitted in the choosing_form
$setNum = isset($_GET['num']) ? $_GET['num'] : null;

// Pass that variable to the setNumber method
$objekat->setNumber($setNum);


function closeCheck() {
	
	// globalize strings and object
	global $objekat, $txt;
	// We use the close property and check is it true
	if($objekat->getClose()) {
		// If it is, it means the user was close
		echo '<br/>' . $txt['it_was_close'];
	}
	else {
		// otherwise, the user wasn't close
		echo '<br/>' . $txt['it_was_not_close'];
	}
}

// Function for outputting statistics
function stats() {
	
	// globalize strings and object
	global $txt, $objekat;

	// Initialize statistics
	$objekat->initializeStats();

	// Show the link to start a new game
	echo '<br/><a href="index.php">'. $txt['start_new_game'] .'</a> ';

	// Print out a heading that says Statistics
	echo '<br/><h2>'. $txt['stats'] .'</h2><br/>'

			//  Output what was the number
	. $txt['the_number_was'] . $objekat->getSessionNumber() . '<br/>'

			// Output when the user started the game
	. $txt['started_guessing'] . $objekat->getStarted() .'<br/>'

			// Show when the user ended the game
	. $txt['ended_guessing'] . $objekat->getEnded() .'<br/>'

			// Show how much time the user spent in seconds
	. $txt['time_spent'] . $objekat->getTimeSpent() . $txt['seconds'] . '<br/>'

			// Show the number of attempts that were made
	. $txt['num_of_attempts'] . $objekat->getRememberCount() .'<br/>'

			// Show attempted numbers
	. $txt['attempts'] .'<br/>';

	// Get the attempted numbers list
	attemptedNumbers();
}
// This function will print attempted numbers in the statistics, with the time when they were submitted
function attemptedNumbers() {

	// globalize strings and object
	global $objekat, $txt;
	
	// start the table and the first row
	echo '<table><tr><td>'. $txt['attempted_number'] .'</td>';

	// Print list of numbers
	$objekat->attemptsNumbersList();

	// End table row and start another one
	echo '</tr><tr><td>'. $txt['time'] .'</td>';

	// Print the time when attempts were made
	$objekat->attemptsNumbersTime();

	// end the row and end the table
	echo '</tr></table>';
}
function guessing_form() {
	
	// Globalize the strings again
	global $txt;
	
	// Echo out the form for guessing the number
		echo '<br/>';
		
		// Print the title
		echo '<p><h2>'. $txt['info'] .':</h2><br/>'
		
		// Print the information
		. $txt['guessing_form_info_text'] .'
		</p>
		<br/>';
		
		// Print the form
  		echo '	<form action="index.php" method="get">
					<input type="number" name="attempt" />
					<input type="submit" name="guess_button" value="'. $txt['guess'] .'" />
					<input type="hidden" name="action" value="guess" />
				</form>';	
}

function choosing_form() {
	
	// Globalize the strings again
	global $txt, $objekat;
	
	// echo out the form for choosing a random number and saving it in the session
		echo '<br/>';
	
		//This is the title
		echo '<p><h2>'. $txt['info'] .':</h2><br/>'
	
		// This code here first prints the information. It uses variables from the top and strings
		. $txt ['choosing_form_info_text'] . $objekat->getRandomNumberFrom() . $txt['and'] . $objekat->getRandomNumberTo() . $txt['you_can_try']. $objekat->getMaxAttempts() . $txt['times'] .'
		</p><br/>';
		
		// Print the form
  		echo '	<form action="index.php" method="get">
  					<input type="submit" name="num" value="'. $txt['choose_number'] .'" />
					<input type="hidden" name="action" value="choose"/>
  				</form>'; 
}






// Just to make sure the action is set
if(isset($get_action)) {
  
  // Switch by action. There are two types of action - choose  and   guess
  switch($get_action) 
  {
    
    // If the action is Choose, do this
  	case 'choose':
  
	  	// Destroying and restarting session just to make sure there are no data left when we start new game
	  	session_destroy();
	  	session_start();
	      
		// Determine the time we started and save it in a session
	    $_SESSION['started'] = date('H:i:s');
	      
		// Create a random number and store it in a session using this function
		$objekat->createRandomNumber();
	        
		// Finish this case
	   	break;
   
   
	// This is what happens if the action is Guess  - when the user inputs a number and clicks Guess
	case 'guess':
  		
		// Globalize the variable we need
		global $get_attempt, $objekat;
      
    	// See if the guess was submitted
        if(isset($_GET['guess_button']))
        {
			global $objekat;
		  	// Save the time of the attempt
          	$_SESSION['attempt'][''.$objekat->getCounter().'']['time'] = date('H:i:s');
        
        	
          	
          	$the_number = $_SESSION['number'];
          	
          	
          	// Write down the attempted number
          	$_SESSION['attempted_numbers'][] = $get_attempt;
          	
          	
          	// The mechanism itself - see if the attempted number and the number from the session are a match. Then parse the value to variable mechanism_check
          	$mechanism_check = $objekat->mechanismCheck($get_attempt, $the_number);
          	
          	// If they are a match, say it
          	if($mechanism_check == TRUE) {
          	
          		// You did it!
          		echo '<h1>'. $txt['you_did_it'] .'</h1>';
          			
          		// Print the statistics
          		$objekat->setShowStats(true);
          	
          		// Do not show the guessing form
          		$objekat->setShowGuessingForm(false);
          	}
          	
          	// If they are not a match, run this bit of code
          	else {
          			
          		// First check if the user exceeded max attempts
          		if($objekat->getMaxAttempts() > $objekat->getCounter()) {
          			// If he DIDN'T, tell him to try again
          			echo '<h1>'. $txt['try_again'] .'</h1>';
          	
          			// Since he's supposed to try again, show him the guessing form
          			$objekat->setShowGuessingForm(true);
          	
          			// Also, help him a little bit
          			$objekat->help($the_number, $get_attempt);
          				
          			// So now we check to see if the number is smaller than it should be
          			if($objekat->getChecker() == true) {
          					
          				// It is, so they should try a bigger number
          				echo $txt['try_bigger_number'];
          					
          				// And see how close they were
          				closeCheck();
          	
          			}
          			else {
          					
          				// But if the number is bigger than it should be, tell them
          				echo $txt['try_smaller_number'];
          					
          				// And check how close they were
          				closeCheck();
          	
          			}
          		}
          			
          		// But if the maximum number of attempts is equal or smaller than the number of attempts, you failed
          		else {
          				
          			// Tell them they are wrong
          			echo '<h1>'. $txt['you_failed'] .'</h1>';
          	
          			// And also print out statistics, because its game over
          			$objekat->setShowStats(true);
          				
          			// Do not show the guessing form
          			$objekat->setShowGuessingForm(false);
          		}
          	}
          	
          	if($objekat->getShowStats()) {
          		stats();
          	}
          	
        }
      
	// Finish case
	break;
      
  } // ends the switch
} // ends the if


// If any of the get_action is set, show the guessing form. Also show it where its stated as true
if (isset($get_action) && $objekat->getShowGuessingForm() == true) {
	// echo out the guessing form
	guessing_form();
}

// If not, then we need to set it with the choosing form. We can not use else here because it will show on the game over screen
if (!isset($get_action)) {
  	// echo out the choosing form
	choosing_form();
}
?>